﻿using Coldairarrow.Business.View;
using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.View
{
    [Route("/View/[controller]/[action]")]
    public class View_WorkloadStatisticsController : BaseApiController
    {
        #region DI

        public View_WorkloadStatisticsController(IView_WorkloadStatisticsBusiness view_WorkloadStatisticsBus)
        {
            _view_WorkloadStatisticsBus = view_WorkloadStatisticsBus;
        }

        IView_WorkloadStatisticsBusiness _view_WorkloadStatisticsBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<View_WorkloadStatistics>> GetDataList(PageInput<WorkloadConditionDTO> input)
        {
            return await _view_WorkloadStatisticsBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<View_WorkloadStatistics> GetTheData(IdInputDTO input)
        {
            return await _view_WorkloadStatisticsBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(View_WorkloadStatistics data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _view_WorkloadStatisticsBus.AddDataAsync(data);
            }
            else
            {
                await _view_WorkloadStatisticsBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _view_WorkloadStatisticsBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 工资信息导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportWorkloadList(WorkloadConditionDTO input)
        {
            List<View_WorkloadStatistics> listdata = await _view_WorkloadStatisticsBus.GetTheDataListAsync(input);

            //创建EXCEL工作薄
            IWorkbook workBook = new XSSFWorkbook();
            //创建sheet1文件表
            ISheet sheet = workBook.CreateSheet("员工工作量统计");

            var expDir = string.Format("{0}Export\\{1}", System.AppDomain.CurrentDomain.BaseDirectory,
                    DateTime.Now.ToString("yyyyMM"));

            if (!Directory.Exists(expDir)) Directory.CreateDirectory(expDir);

            string filePath = string.Format("{0}\\CD{1}.xlsx", expDir, DateTime.Now.ToString("yyyyMMddHHmmss"));

            //创建表头
            IRow header = sheet.CreateRow(0);
            ICell cell = header.CreateCell(0);
            cell.SetCellValue("日期");
            cell = header.CreateCell(1);
            cell.SetCellValue("部门");
            cell = header.CreateCell(2);
            cell.SetCellValue("工序");
            cell = header.CreateCell(3);
            cell.SetCellValue("姓名");
            cell = header.CreateCell(4);
            cell.SetCellValue("扫描类型");
            cell = header.CreateCell(5);
            cell.SetCellValue("扫码次数");
            cell = header.CreateCell(6);
            cell.SetCellValue("数量");

            for (int i = 0; i < listdata.Count; i++)
            {
                IRow rowData = sheet.CreateRow(i + 1);
                ICell cel = rowData.CreateCell(0);
                cel.SetCellValue(listdata[i].SendTime.ToString());

                cel = rowData.CreateCell(1);
                cel.SetCellValue(listdata[i].DeptName.ToString());

                cel = rowData.CreateCell(2);
                cel.SetCellValue(listdata[i].ProcedureName.ToString());

                cel = rowData.CreateCell(3);
                cel.SetCellValue(listdata[i].RealName.ToString());

                cel = rowData.CreateCell(4);
                cel.SetCellValue(listdata[i].Type.ToString());

                cel = rowData.CreateCell(5);
                cel.SetCellValue(listdata[i].Nums.ToString());

                cel = rowData.CreateCell(6);
                cel.SetCellValue(listdata[i].StatisticalQuantity.ToString());

            }
            //工作流写入，通过流的方式进行创建生成文件
            using (MemoryStream stream = new MemoryStream())
            {
                workBook.Write(stream);
                byte[] buffer = stream.ToArray();

                return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", string.Format("员工工作量统计.xlsx", DateTime.Now.ToString("yyyyMMddHHmmss")));
            }
        }

        #endregion
    }
}